package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import javax.mail.Session;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

@Slf4j
public class EmailUtils {
    // 全局复用的 Session 实例
    private static final Session SMTP_SESSION;

    private static final String username = "service@ubpay.ai";
    private static final String password = "2bvW5p9+G8";

    static {
        Session tempSession = null;
        try {
            // 1. 配置 SMTP 服务器参数
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtpout.secureserver.net");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.connectiontimeout", "30000");
            props.put("mail.smtp.timeout", "30000");

            // 2. 创建带认证信息的 Session
            tempSession = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password); // 替换为实际信息
                }
            });
            tempSession.setDebug(false);

        } catch (Exception e) {
            throw new RuntimeException("SMTP_SESSION 初始化失败：" + e.getMessage(), e);
        }
        SMTP_SESSION = tempSession;
    }

    // 发送邮件的工具方法（兼容低版本 commons-email）
    public static void sendHtmlEmail(String toEmail, String code) throws EmailException {
        if (SMTP_SESSION == null) {
            throw new EmailException("SMTP 会话未初始化");
        }
        HtmlEmail email = new HtmlEmail();
        String imagePath = "http://api.ubpay.ai/infra/file/1747439750615781378/get/cw-interlace/normal/7383f88bc15fdd7de1f0ea09eb9b186b2d829e9111397852cda7ddc160049a79.png";
        String cid = email.embed(imagePath, "logo");
        email.setMailSession(SMTP_SESSION);
        String emailSubject = "Your UBPay Verification Code";
        String emailContent =
            "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <style type=\"text/css\">\n" +
                "        @media screen and (max-width: 600px) {\n" +
                "            .container { width: 100%!important; }\n" +
                "            .content { padding: 15px!important; }\n" +
                "        }\n" +
                "        .hidden-content { display: none; }\n" +
                "        .toggle-checkbox { display: none; }\n" +
                "        .toggle-checkbox:checked + .hidden-content { display: block; }\n" +
                "        .toggle-checkbox:checked ~ .content { display: none; }\n" +
                "        .toggle-button {\n" +
                "            color: blue;\n" +
                "            text-decoration: underline;\n" +
                "            text-align: center;\n" +
                "            display: inline-block;\n" +
                "            cursor: pointer;\n" +
                "        }\n" +
                "        .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .full-content .hidden-content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content .hidden-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .content {\n" +
                "            height: 400px;\n" +
                "            .position {\n" +
                "                padding: 200px 0;\n" +
                "            }\n" +
                "        }\n" +
                "        .container {\n" +
                "            display: flex;\n" +
                "            flex-direction: column;\n" +
                "            justify-content: center;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <input type=\"checkbox\" id=\"toggle-1\" class=\"toggle-checkbox\">\n" +
                "        <div class=\"content\">\n" +
                "            <div class=\"position\">\n" +
                "                <p>...</p>\n" +
                "                <span>[邮件部分隐藏]</span>\n" +
                "                <!-- 展开按钮 -->\n" +
                "                <label for=\"toggle-1\" class=\"toggle-button\">点击展开内容</label>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "        <div class=\"full-content\">\n" +
                "            <!-- 隐藏内容 -->\n" +
                "            <div class=\"hidden-content\">\n" +
                "\t\t\t\t<img src='cid:" + cid + "' width='70' alt='logo' />\n" +
                "                <p><strong style=\"font-size: 18px;\">Dear Valued Customer,</strong></p>\n" +
                "                <p>Your verification code is: <strong style=\"font-size: 22px;\">" + code + "</strong></p>\n" +
                "                <p><br></p>\n" +
                "                <p>This code will expire within 5 minutes. Never provide this code to anyone.\n" +
                "\t\t\t\tIf you have any question or need support, please contact our customer service team immediately.</p>\n" +
                "                <p><br></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">Best regards,</strong></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">The UBPay Team</strong></p>\n" +
                "\t\t\t\t<p style=\"font-size: 16px;font-style: italic;\">This is an automated message, please do not reply.</p>\n" +
                "                <p style=\"font-size: 14px;\">© 2025 UBPay, All Rights Reserved.</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>\n";
        try {
            //目的邮件
            email.addTo(toEmail);
            //发送者
            email.setFrom(username, "UBPay");
            //标题
            email.setSubject(emailSubject);
            //邮件内容
            email.setHtmlMsg(emailContent);
            email.setCharset("UTF-8");
            email.send();
        } catch (EmailException ex) {
            log.info("邮件发送失败:{}", ex.getMessage());
        }
    }

    public static void main(String[] args)throws EmailException {
        sendHtmlEmail("1902339614@qq.com", "123456");
    }
}
